package com.proxy.router;

import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.ast.statement.SQLShowTablesStatement;
import com.alibaba.druid.sql.ast.statement.SQLUseStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.*;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.parser.SQLStatementParser;
import com.proxy.parser.ParsedResult;
import com.proxy.parser.result.*;
import com.proxy.parser.visitor.*;

import java.util.List;

/**
 * Created by liufish on 16/11/1.
 */
public class SQLRouteEngine {


    /**
     * 二级路由
     */
   private final static SQLRouteDetail routeDetail = new SQLRouteDetail();

    /**
     * SQL路由.
     *
     * @param logicSql 逻辑SQL
     * @return 路由结果
     */
    public SQLRouteResult route(String logicSql)  {

        SQLRouteResult result = new SQLRouteResult();
        result.setOriginalSql(logicSql);

        SQLStatementParser parser = new MySqlStatementParser(logicSql);
        SQLStatement sqlStatement = parser.parseStatement();

        /**
         * select
         */
        if(sqlStatement instanceof SQLSelectStatement){
            ParsedResult parsedResult =  new SelectResult();
            SelectVisitor visitor =   new SelectVisitor(parsedResult);
            sqlStatement.accept(visitor);
            result.setParsedResult(parsedResult);
            this.routeDetail.routeSelect(result);
            return result;
        }

        /**
         * show
         */
        if(sqlStatement instanceof MySqlShowDatabasesStatement){
            ParsedResult parsedResult =  new ShowDatabasesResult();
            ShowDatabasesVisitor visitor = new ShowDatabasesVisitor(parsedResult);
            sqlStatement.accept(visitor);
            result.setParsedResult(parsedResult);
            return result;
        }

        if(sqlStatement instanceof MySqlShowVariantsStatement){
            ParsedResult parsedResult =  new ShowVariantsResult();
            ShowVariantsVisitor visitor = new ShowVariantsVisitor(parsedResult);
            sqlStatement.accept(visitor);
            result.setParsedResult(parsedResult);
            return result;
        }

        if(sqlStatement instanceof SQLShowTablesStatement){
            ParsedResult parsedResult =  new ShowTablesResult();
            ShowTablesVisitor visitor = new ShowTablesVisitor(parsedResult);
            sqlStatement.accept(visitor);
            result.setParsedResult(parsedResult);
            return result;
        }

        if(sqlStatement instanceof MySqlShowCreateTableStatement){
            ParsedResult parsedResult =  new ShowCreateTableResult();
            ShowCreateTableVisitor visitor = new ShowCreateTableVisitor(parsedResult);
            sqlStatement.accept(visitor);
            result.setParsedResult(parsedResult);
            return result;
        }

        if(sqlStatement instanceof MySqlShowColumnsStatement){
            ParsedResult parsedResult =  new ShowColumnsResult();
            ShowColumnsVisitor visitor = new ShowColumnsVisitor(parsedResult);
            sqlStatement.accept(visitor);
            result.setParsedResult(parsedResult);
            return result;
        }

        if(sqlStatement instanceof MySqlShowIndexesStatement){
            ParsedResult parsedResult =  new ShowIndexesResult();
            ShowIndexesVisitor visitor = new ShowIndexesVisitor(parsedResult);
            sqlStatement.accept(visitor);
            result.setParsedResult(parsedResult);
            return result;
        }






        /**
         * for set
         */
        if(sqlStatement instanceof MySqlSetNamesStatement){
            ParsedResult parsedResult = new SetNamesResult();
            SetNamesVisitor visitor = new SetNamesVisitor(parsedResult);
            sqlStatement.accept(visitor);
            result.setParsedResult(parsedResult);
            return result;
        }
        if(sqlStatement instanceof MySqlSetTransactionStatement){
            ParsedResult parsedResult = new SetTransactionResult();
            SetTransactionVisitor visitor = new SetTransactionVisitor(parsedResult);
            sqlStatement.accept(visitor);
            result.setParsedResult(parsedResult);
            return result;
        }

        //set autocommit=1



        /**
         * use
         */
        if (sqlStatement instanceof SQLUseStatement) {
            ParsedResult parsedResult = new UseParsedResult();
            UseVisitor visitor = new UseVisitor(parsedResult);
            sqlStatement.accept(visitor);
            result.setParsedResult(parsedResult);
            return result;
        }




        return  result;
    }


    /**
     * SQL路由.
     *
     * @param logicSql 逻辑SQL
     * @return 路由结果
     */
    public SQLRouteResult route(String logicSql,  List<Object> parameters)  {
        return  null;
    }

}
